package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.PortUnreachableException;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //找出ShoppingCart 中userid 与user id相等的情况
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }
    //添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //添加购物车 利用userid进行条件判断数据库有无这个菜品dishid和setmealid
        //如果有其中之一的id 就在原本的数量number+1,没有的话就设置number为1
        //首先获取user中id
        Long id = BaseContext.getCurrentId();
        shoppingCart.setUserId(id);
        //条件构造来寻找出有无这个对象
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,id);
        //没点菜品就点的是套餐
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //如果one等于null就证明是购物车里没有这个数据 设置number=1
       if(one==null){
           shoppingCart.setNumber(1);
           //进行添加操作
           shoppingCartService.save(shoppingCart);
           return R.success(shoppingCart);
       }
           //如果one不等于null说明购物车有这个数据,这种情况无需增加,只需要将购物车的number+1
           one.setNumber(one.getNumber()+1);
           shoppingCartService.updateById(one);
          return R.success(one);
    }

    //取消购物车
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //先找出shoppingcart的userid与user 中id相等的情况下进行操作
        //然后找出取消的是菜品还是套餐
        //如果shoppingcart的nubmer为1 则删除这条数据
        //如果shoppingcart的nubmer不为1 则将这条数据的number进行-1操作
        //select * from shoppingcart where userid=? and dish_id=?/setmeal_id=?
        //找出userid
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userid);
        Long dishId = shoppingCart.getDishId();
        //判断操作的是菜品还是套餐
        if(dishId!=null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //找出操作的这条shoppingcart数据
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one.getNumber()==1){
            one.setNumber(0);
            shoppingCartService.removeById(one.getId());
            return R.success(one);
        }else {
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        //找出shoppingcart中userid与 user id相等的数据然后进行删除
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功！！！");
    }
}
